﻿//打开字滑入效果
window.onload = function () {
	$(".connect p").eq(0).animate({ "left": "0%" }, 600);
	$(".connect p").eq(1).animate({ "left": "0%" }, 400);
};
//jquery.validate表单验证
$(document).ready(function () {
	//登陆表单验证
	$("#loginForm").validate({
		rules: {
			telephone: {
				required: true,
				phone_number: true, // 自定义的规则
				digits: true, // 整数
			},
			password: {
				required: true,
				minlength: 3,
				maxlength: 32,
			},
		},
		//错误信息提示
		messages: {
			telephone: {
				required: "请输入手机号码",
				digits: "请输入正确的手机号码",
			},
			password: {
				required: "必须填写密码",
				minlength: "密码至少为3个字符",
				maxlength: "密码至多为32个字符",
			},
		},

	});

	// 短信表单校验
	$("#loginFormphone").validate({
		rules: {
			telephone: {
				required: true,
				phone_number: true, // 自定义的规则
				digits: true, // 整数
			},
			code: {
				required: true,
				digits: true, // 整数
				minlength: 6,
				maxlength: 6,
			},
		},
		//错误信息提示
		messages: {
			telephone: {
				required: "请输入手机号码",
				digits: "请输入正确的手机号码",
			},
			code: {
				required: "输入验证码",
				digits: "请输入正确的验证码",
			},
		},

	});
	
	//注册表单验证
	$("#registerForm").validate({
		rules: {
			username: {
				required: true, // 是否必填
				minlength: 1, // 最少6个字符
				maxlength: 16, // 最多32个字符
				// remote: {
				// 	// 这个地址，是当注册页面填完用户名时，
				// 	// 直接请求后端，看你填写的帐号是否已经是老用户/被注册过的意思，
				// 	// 如果后端没有做校验，则不需要这个。
				// 	url: "http://xxxxx/xxx",
				// 	type: "post",
				// },
			},
			password: {
				required: true,
				minlength: 3,
				maxlength: 32,
			},
			code: {
				required: true,
				digits: true, // 整数
				minlength: 6,
				maxlength: 6,
			},
			confirm_password: {
				required: true,
				minlength: 3,
				equalTo: '.password'
			},
			phone_number: {
				required: true,
				phone_number: true, // 自定义的规则
				digits: true, // 整数
			}
		},
		// 错误信息提示
		messages: {
			username: {
				required: "必须填写用户名",
				minlength: "用户名至少为1个字符",
				maxlength: "用户名至多为16个字符",
				remote: "用户名已存在",
			},
			password: {
				required: "必须填写密码",
				minlength: "密码至少为3个字符",
				maxlength: "密码至多为32个字符",
			},
			code: {
				required: "输入验证码",
				email: "请输入正确的验证码",
			},
			confirm_password: {
				required: "请再次输入密码",
				minlength: "确认密码不能少于3个字符",
				equalTo: "两次输入密码不一致", // 与另一个元素相同
			},
			phone_number: {
				required: "请输入手机号码",
				digits: "请输入正确的手机号码",
			},

		},
	});

	//注册表单验证
	$("#businessregisterForm").validate({
		rules: {
			loginname: {
				required: true, // 是否必填
				minlength: 1, // 最少6个字符
				maxlength: 16, // 最多32个字符
				remote: {
					url: "/business/checkloginname",     //后台处理程序
					type: "post",               //数据发送方式
					dataType: "json",           //接受数据格式
					data: {                     //要传递的数据
						loginname: function() {
							return $("#loginname").val().trim();
						}
					}
				}
			},
			businessname: {
				required: true, // 是否必填
				minlength: 1, // 最少6个字符
				maxlength: 16, // 最多32个字符
				remote: {
					url: "/business/checkbusinessname",    //后台处理程序
					type: "post",               //数据发送方式
					dataType: "json",           //接受数据格式
					data: {                     //要传递的数据
						businessname: function() {
							return $("#businessname").val().trim();
						}
					}
				}
			},
			password: {
				required: true,
				minlength: 3,
				maxlength: 32,
			},
			code: {
				required: true,
				digits: true, // 整数
				minlength: 6,
				maxlength: 6,
			},
			confirm_password: {
				required: true,
				minlength: 3,
				equalTo: '.password'
			},
			phone_number: {
				required: true,
				phone_number: true, // 自定义的规则
				digits: true, // 整数
			}
		},
		// 错误信息提示
		messages: {
			loginname: {
				required: "必须填写登录名",
				minlength: "用户名至少为1个字符",
				maxlength: "用户名至多为16个字符",
				remote: "登录名已存在",
			},
			businessname: {
				required: "必须填写店铺名称",
				minlength: "用户名至少为1个字符",
				maxlength: "用户名至多为16个字符",
				remote: "店铺名称已存在",
			},
			password: {
				required: "必须填写密码",
				minlength: "密码至少为3个字符",
				maxlength: "密码至多为32个字符",
			},
			code: {
				required: "输入验证码",
				email: "请输入正确的验证码",
			},
			confirm_password: {
				required: "请再次输入密码",
				minlength: "确认密码不能少于3个字符",
				equalTo: "两次输入密码不一致", // 与另一个元素相同
			},
			phone_number: {
				required: "请输入手机号码",
				digits: "请输入正确的手机号码",
			},

		},
	});


	// 添加自定义验证规则
	jQuery.validator.addMethod("phone_number", function (value, element) {
		var phone_length = value.length;
		// 手机号正则表达式
		var phone_reg = /^[1][3,4,5,7,8,9][0-9]{9}$/
		return this.optional(element) || (phone_length == 11 && phone_reg.test(value));
	}, "手机号码格式错误");

	// 添加自定义验证规则
	jQuery.validator.addMethod("telephone", function (value, element) {
		var phone_length = value.length;
		// 手机号正则表达式
		var phone_reg = /^[1][3,4,5,7,8,9][0-9]{9}$/
		return this.optional(element) || (phone_length == 11 && phone_reg.test(value));
	}, "手机号码格式错误");

});

// 用户注册
function signup() {
	var validRet = !!$("#registerForm").valid();
	if (!validRet) {
		return;
	}
	$.ajax({
		type: "post",//方法类型
		dataType: "json",//服务器返回的数据类型
		url: "/user/signup" ,//控制器url
		data: $('#registerForm').serialize(),
		success: function (result) {
			console.log(result);//测试是否有数据
			if (result.code == 200) {//200正确
				if (result.data.code != 6) {
					document.getElementById("msg").innerHTML = "<h3 style='color:#f112a6 ; font-size:17px'>"+result.data.ms+"</h3>";
				}else{
					alert(result.data.ms);
					window.location.href = 'index.html';
				}
			}
		},
		error : function() {
			alert("注册失败！");
		}
	});
}

// 商家注册
function businesssignup() {
	var validRet = !!$("#businessregisterForm").valid();
	if (!validRet) {
		return;
	}
	$.ajax({
		type: "post",//方法类型
		dataType: "json",//服务器返回的数据类型
		url: "/business/signup" ,//控制器url
		data: $('#businessregisterForm').serialize(),
		success: function (result) {
			console.log(result);//测试是否有数据
			if (result.code == 200) {//200正确
				if (result.data.code != 6) {
					document.getElementById("msg").innerHTML = "<h3 style='color:#f112a6 ; font-size:17px'>"+result.data.ms+"</h3>";
				}else{
					alert(result.data.ms);
					window.location.href = 'index.html';
				}
			}
		},
		error : function() {
			alert("注册失败！");
		}
	});
}


// 用户登录
function signin() {
	var validRet = !!$("#loginForm").valid();
	if (!validRet) {
		return;
	}
	$.ajax({
		type: "post",//方法类型
		dataType: "json",//服务器返回的数据类型
		url: "/user/signin" ,//控制器url
		data: $('#loginForm').serialize(),
		success: function (result) {
			console.log(result);//测试是否有数据
			if (result.code == 200) {//200正确
				if (result.data.code != 9) {
					document.getElementById("msg").innerHTML = "<h3 style='color:#f112a6 ; font-size:17px'>"+result.data.ms+"</h3>";
				}else{
					$.session.clear();
					// object 转 json
					var userinfo = JSON.stringify(result.data.user);
					$.session.set("user",userinfo);
					// json 转 object
					console.log(JSON.parse($.session.get("user")).id);
					window.location.href = 'main.html';
				}
			}
		},
		error : function() {
			alert("登录失败！");
		}
	});
}

// 用户短信登录
function signbyphone() {
	var validRet = !!$("#loginFormphone").valid();
	if (!validRet) {
		return;
	}
	$.ajax({
		type: "post",//方法类型
		dataType: "json",//服务器返回的数据类型
		url: "/user/signbyphone" ,//控制器url
		data: $('#loginFormphone').serialize(),
		success: function (result) {
			console.log(result);//测试是否有数据
			if (result.code == 200) {//200正确
				if (result.data.code != 15) {
					document.getElementById("msg").innerHTML = "<h3 style='color:#f112a6 ; font-size:17px'>"+result.data.ms+"</h3>";
				}else{
					$.session.clear();
					// object 转 json
					var userinfoT = JSON.stringify(result.data.user);
					$.session.set("user", userinfoT);
					// json 转 object
					window.location.href = 'main.html';
				}
			}
		},
		error : function() {
			alert("登录失败！");
		}
	});
}

var countdown=60;
var i = 0;

// 用户注册手机验证
function settime(obj) {
	if (i == 0){
		// 对code先不校验，因为还没有填入验证码
		var validRet = !!$("#registerForm").validate({ignore: $('#code')});
		if (!validRet) {
			return;
		}
		var telPhone = document.getElementById("telephone").value;
		$.ajax({
			type: "GET",//方法类型
			dataType: "json",//服务器返回的数据类型
			url: "/user/telephone" ,//控制器url
			data: "telPhone="+telPhone,
			success: function (result) {
				console.log(result);//测试是否有数据
				if (result.code == 200) {//200正确
					// 设个是手机号已经被注册
					if (result.data.code != 2) {
						document.getElementById("msg").innerHTML = "<h3 style='color:#f112a6 ; font-size:17px'>"+result.data.ms+"</h3>";
					}else{
						alert(result.data.ms);
					}
				}
			},
			error : function() {
				alert("验证码发送失败");
			}
		});
		i = 2;
	}
	if (countdown == 0) {
		obj.removeAttribute("disabled");
		obj.value="获取验证码";
		countdown = 60;
		return;
	} else {
		obj.setAttribute("disabled", true);
		obj.value="重新发送(" + countdown + "s)";
		countdown--;
	}
	setTimeout(function() {
			settime(obj) }
		,1000)
}

// 商家注册手机验证
function settimebusiness(obj) {
	if (i == 0){
		// 对code先不校验，因为还没有填入验证码
		var validRet = !!$("#businessregisterForm").validate({ignore: $('#code')});
		if (!validRet) {
			return;
		}
		var telPhone = document.getElementById("telephone").value;
		$.ajax({
			type: "GET",//方法类型
			dataType: "json",//服务器返回的数据类型
			url: "/business/telephone" ,//控制器url
			data: "telPhone="+telPhone,
			success: function (result) {
				console.log(result);//测试是否有数据
				if (result.code == 200) {//200正确
					// 设个是手机号已经被注册
					if (result.data.code != 18) {
						document.getElementById("msg").innerHTML = "<h3 style='color:#f112a6 ; font-size:17px'>"+result.data.ms+"</h3>";
					}else{
						alert(result.data.ms);
					}
				}
			},
			error : function() {
				alert("验证码发送失败");
			}
		});
		i = 2;
	}
	if (countdown == 0) {
		obj.removeAttribute("disabled");
		obj.value="获取验证码";
		countdown = 60;
		return;
	} else {
		obj.setAttribute("disabled", true);
		obj.value="重新发送(" + countdown + "s)";
		countdown--;
	}
	setTimeout(function() {
			settime(obj) }
		,1000)
}

// 用户登录短信验证
function settimes(obj) {
	if (i == 0){
		// 对code先不校验，因为还没有填入验证码
		var validRet = !!$("#loginFormphone").validate({ignore: $('#code')});
		if (!validRet) {
			return;
		}
		var telPhone = document.getElementById("telephone").value;
		$.ajax({
			type: "GET",//方法类型
			dataType: "json",//服务器返回的数据类型
			url: "/user/telephonelogin" ,//控制器url
			data: "telPhone="+telPhone,
			success: function (result) {
				console.log(result);//测试是否有数据
				if (result.code == 200) {//200正确
					if (result.data.code != 13) {
						document.getElementById("msg").innerHTML = "<h3 style='color:#f112a6 ; font-size:17px'>"+result.data.ms+"</h3>";
					}else{
						alert(result.data.ms);
					}
				}
			},
			error : function() {
				alert("验证码发送失败");
			}
		});
		i = 2;
	}
	if (countdown == 0) {
		obj.removeAttribute("disabled");
		obj.value="获取验证码";
		countdown = 60;
		return;
	} else {
		obj.setAttribute("disabled", true);
		obj.value="重新发送(" + countdown + "s)";
		countdown--;
	}
	setTimeout(function() {
			settime(obj) }
		,1000)
}